﻿using B.S.BaseData.ErrorCode;
using B.S.BaseData.Manage.Api.Read.Apilaction.Command.ProductModelCommand;
using B.S.BaseData.Manage.Api.Read.DTO;
using B.S.Passport.Domain;
using DAL;
using MediatR;
using Microsoft.Data.SqlClient;
using System;
using System.Collections.Generic;
using System.Threading;
using System.Threading.Tasks;

namespace B.S.BaseData.Manage.Api.Read.Apilaction.CoomandHandler.ProductModelCommand
{
    /// <summary>
    /// 产品显示全部
    /// </summary>
    public class ProductQuserModelCommandHandler : IRequestHandler<ProductQuserModelCommand, ApiReqult<ApIPageing<ProductModel>>>
    {
        private readonly DBHelper bHelper;

        public ProductQuserModelCommandHandler(DBHelper bHelper)
        {
            this.bHelper = bHelper;
        }
        /// <summary>
        /// 产品显示全部
        /// </summary>
        /// <param name="request">请求</param>
        /// <param name="cancellationToken">取消</param>
        /// <returns>返回任务</returns>
        public Task<ApiReqult<ApIPageing<ProductModel>>> Handle(ProductQuserModelCommand request, CancellationToken cancellationToken)
        {
            ApiReqult<ApIPageing<ProductModel>> result = new ApiReqult<ApIPageing<ProductModel>>();
            try
            {
                int skip = (request.PageIdex - 1) * request.PageSize;
                int take = request.PageSize;

                // 构建查询总数的 SQL 语句
                string countSql = $"SELECT COUNT(*) FROM ProductModel";
                if (request.ProductId.HasValue)
                {
                    countSql += $" WHERE ProductId = {request.ProductId.Value}";
                }
                else if (!string.IsNullOrEmpty(request.ProductName))
                {
                    countSql += $" WHERE ProductName LIKE '%{request.ProductName}%'";
                }

                // 获取总数
                int totalCount = Convert.ToInt32(bHelper.Scalar(countSql));

                // 构建分页查询的 SQL 语句
                string sql = $"SELECT * FROM ProductModel";
                if (request.ProductId.HasValue)
                {
                    sql += $" WHERE ProductId = {request.ProductId.Value}";
                }
                else if (!string.IsNullOrEmpty(request.ProductName))
                {
                    sql += $" WHERE ProductName LIKE '%{request.ProductName}%'";
                }

                sql += $" ORDER BY ProductId OFFSET {skip} ROWS FETCH NEXT {take} ROWS ONLY";

                List<ProductModel>? hoModes = bHelper.GetList<ProductModel>(sql);

                if (hoModes != null)
                {
                    ApIPageing<ProductModel> pageData = new ApIPageing<ProductModel>();
                    pageData.TotalCount = totalCount;
                    pageData.TotalPage = (int)Math.Ceiling(pageData.TotalCount * 1.0 / request.PageSize);
                    pageData.List = hoModes;
                    result.Data = pageData;
                    result.Msg = ApiEnum.查询成功.ToString();
                }
                else
                {
                    result.Msg = ApiEnum.查询失败.ToString();
                    result.Code = ApiEnum.查询失败;
                }

            }
            catch (SqlException ex)
            {
                result.Msg = ApiEnum.查询失败.ToString();
                result.Code = ApiEnum.查询失败;
            }
            return Task.FromResult(result);
        }
    }
}